package com.chpro.lang;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import sun.util.logging.resources.logging;

public class DateUtil {
	static Log log = LogFactory.getLog(DateUtil.class);
	static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/**
	 * 根据传进来的日期  得到这个月的最后一天的日期
	 * @param date
	 */
	public static String getLastDate(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String currentDate = sdf.format(date);
		
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR,Integer.parseInt(currentDate.substring(0,4)));
		cal.set(Calendar.MONTH,Integer.parseInt( currentDate.substring(5,7)));
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		Date lastDate = cal.getTime();
		return sdf.format(lastDate);
	}
	
	/**
	 * 根据传进来的日期  得到这个月的第一天的日期
	 * @param date
	 * @return
	 */
	public static String getFirstDate(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String currentDate = sdf.format(date);
		
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR,Integer.parseInt(currentDate.substring(0,4)));
		cal.set(Calendar.MONTH,Integer.parseInt( currentDate.substring(5,7)));
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
	    Date firstDate = cal.getTime();
		return sdf.format(firstDate);
	}
	
	/**
	 * 
	 * @param num 得到当前日期 前几个月的日期
	 * @return
	 */
	public static List getAgo(int num){
		List<String> list = new ArrayList<String>();
		
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String currentTime = sdf.format(date);
		
		Integer year = Integer.parseInt(currentTime.substring(0,4));
		Integer month = Integer.parseInt(currentTime.substring(5,7));

		for(int i=0;i<num;i++){
			month--;
			if(month==0){
				month=12;
				year--;
			}
			list.add(year+"-"+String.format("%02d", month));
		}

		return list;
	}
	
	/**
	 * 取两个String 类型字符串的天数
	 * @param startDate 开始日期
	 * @param endDate 结束日期
	 * @return 返回相差的天数
	 */
	public static Integer daysBetweenDate(String _startDate,String _endDate){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Long interval = null;
		try{
			Date startDate = sdf.parse(_startDate);
			Date endDate = sdf.parse(_endDate);
			if (startDate == null || endDate == null)
			{
				return null;
			}
			interval = endDate.getTime() - startDate.getTime();
			interval = interval / (24 * 60 * 60 * 1000);
			return interval.intValue();
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 取得当前日期 年月日
	 * @return
	 */
	public static String getStringDate() {
		return sdf.format(new Date());
	}
	
	/**
	 * 取得当前日期 年月日 时分秒
	 * @return
	 */
	public static String getStringDateTtime() {
		return sdfTime.format(new Date());
	}
	
	
	
	/**
	 * 根据传进来的日期  得到这个月的最后一天的日期
	 * @param date
	 */
	public static String getUpperMonthLastDate(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String currentDate = sdf.format(date);
		
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR,Integer.parseInt(currentDate.substring(0,4)));
		cal.set(Calendar.MONTH,Integer.parseInt( currentDate.substring(5,7)));
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		cal.add(Calendar.MONTH, -1);
		Date lastDate = cal.getTime();
		return sdf.format(lastDate);
	}
	
	/**
	 * 根据传进来的日期  得到这个月的第一天的日期
	 * @param date
	 * @return
	 */
	public static String getUpperMonthFirstDate(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String currentDate = sdf.format(date);
		
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR,Integer.parseInt(currentDate.substring(0,4)));
		cal.set(Calendar.MONTH,Integer.parseInt( currentDate.substring(5,7)));
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.add(Calendar.MONTH, -1);
	    Date firstDate = cal.getTime();
		return sdf.format(firstDate);
	}
	
	/**
	 * 根据 传进来的年月 得到 这个日期的最后一天
	 * @param year 年
	 * @param month 月
	 * @return
	 */
	public static String getLastDate(String year,String month){
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM").parse(year+"-"+month);
		} catch (Exception e) {
			log.info("传进来的日期不正确");
			e.printStackTrace();
		}
		return getLastDate(date);
	}
	
	/**
	 * 根据 传进来的年月 得到 这个日期的第一天
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getFirstDate(String year,String month){
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM").parse(year+"-"+month);
		} catch (Exception e) {
			log.info("传进来的日期不正确");
			e.printStackTrace();
		}
		return getFirstDate(date);
	}
}
